import Game.{Computer, Fist, User}

import scala.concurrent.duration.Duration.Inf.unit
import scala.util.Random

object Game {
  def verify( seq:Any,roles:Array[String]): Boolean ={
    if (seq.isInstanceOf[Int] ){
      print("fucccc")
      var v:Int = seq.asInstanceOf[Int]
      if(v>roles.length||v<1){
        print("请输入序号1到"+roles.length)
        if(verify(scala.io.StdIn.readLine(),roles)){
          return true
        }
        //v = seq.asInstanceOf[Int]
      }
      return true;
      //seq = scala.io.StdIn.readLine()
    }else if(seq == "n"){
      return false
    }
    print("~~请输入序号1到3")
    verify(scala.io.StdIn.readInt(),roles)
  }



  def  enterGame(content:Array[String]):Unit = {
    print("请选择对战角色:(")

    var i = 0;
    for (elem <- content) {
      i += 1
      print(i + ".")
      print(elem)
    }
    println(")")

  }


  def selectRole(roles:Array[String],content2:List[Fist]):String = {
    var seq = scala.io.StdIn.readInt()
    if (verify(seq, roles)) {
      var v = seq.asInstanceOf[Int]
      var role: String  = roles(v - 1)
      //role1 = role
      print("你已选择与：" + role+"对战")

      return role
    }else{


      return null
      //System.exit(0)
    }
  }


  def main(args: Array[String]): Unit = {
    print("----------欢迎进入游戏世界-----------------\n*********************" +
      "\n*********猜拳,开始**********\n******************************")

    print("\n")
    print("\n")
    print("\n")
    var roles = Array("刘备", "关羽", "张飞")
    enterGame(roles)

    var fists = List(Fist("剪刀",1),Fist("石头",2),Fist("布",3))

    var role = selectRole(roles,fists)
    var computer = new Computer(role,0)


    var user:User = new User("我",0)
    if (role == null||"".equals(role)){
      //计分
      //computeScore(role:String,times:Int,computer: Computer,user: User)
      println("你放弃选择对战角色,自动退出游戏")
      System.exit(0)

    }else{
      play(role,fists, computer, user)
      computeScore(role:String,computer: Computer,user: User)
    }

  }

  private def play(role:String, fists: List[Fist], computer: Computer, user: User):Unit = {
    println("要开始么?y/n")
    var answer = scala.io.StdIn.readLine()
    if (answer == "y") {
      println("请出拳!1.剪刀 2.石头 3.布")

      user.showFist(fists)
      computer.showFist(fists);
      computer.compare(user)
      println("是否开始下一轮(y/n)")
      var answer = scala.io.StdIn.readLine()
      if (answer == "y"){

        //继续对战
        play(role, fists, computer, user)
      }else{
        //计分
        println("输入非y,自动退出游戏")
      }
    } else {
      //计分
      println("你放弃选择对战角色,自动退出游戏")
      //System.exit(0)
    }
  }

  def computeScore(role:String,computer: Computer,user: User):Unit={
    println("退出游戏！")
    println("-----------------------------------------")
    println(role+" VS "+"游客")
    println("对战次数"+computer.times+"次")
    println("\n")
    println("姓名   得分   胜局   和局   负局")
    println("游客   "+user.score+"   "+user.victories+"   "+user.draw+"   "+user.fails)
    println(computer.name+"   "+computer.score+"   "+computer.victories+"   "+computer.draw+"   "+computer.fails)
    System.exit(0)
  }

  class User(name1:String,score1:Int){
    var score :Int = 0
    //胜局数
    var victories = 0;
    //负局数
    var fails = 0;
    //和局数
    var draw = 0;
    var fist:Fist = null
    //对战次数
    var times = 0

    def showFist(fists:List[Fist]): Unit = {
      times+=1
      var f = scala.io.StdIn.readInt()
      if(f>fists.length||f<1){
        println("输入不符合规范,默认出"+fists(fists.length-1).name)
        fist = fists(fists.length-1)
      }else{
        fist = fists(f-1)
        println("你出拳:"+fist.name)
      }
    }
  }

  class Computer(name1:String,score1:Int){

    var name = name1
    var score = 0;
    var value = 0;
    //胜局数
    var victories = 0;
    //负局数
    var fails = 0;
    //和局数
    var draw = 0;

    //对战次数
    var times = 0
    var fist:Fist = null
    def showFist(fists:List[Fist]): Unit = {
      times+=1
      fist = fists(Random.nextInt(fists.length))
      println(name1+"出拳:"+fist.name)
    }
    def compare(rival:User): Unit ={
       var flag = 0
        //电脑胜
        if((fist.name == "剪刀" && rival.fist.name == "布")||
          (fist.name == "石头" && rival.fist.name == "剪刀")||(fist.name == "布" && rival.fist.name == "剪刀")){
          score += 2
          //电脑胜局数+1,我败局数+1
          victories+=1
          rival.fails+=1
          flag = 1
          println("结果:负了,不要气馁")
        }
        //我胜
        else if((fist.name == "剪刀" && rival.fist.name == "石头")
          ||(fist.name == "石头" && rival.fist.name == "布")||(fist.name == "布" && rival.fist.name == "剪刀")){
          rival.score += 2
          //我胜局数+1,电脑败局数+1
          fails+=1
          rival.victories+=1
          flag = -1
          println("结果:恭喜,你赢啦!")
        }
        //和局
        else if(fist.name == rival.fist.name){
          score +=1
          rival.score += 1

          //电脑与我和局数各+1
          draw+=1
          rival.draw+=1
          println("结果:和局!下次继续努力")
        }

        //return flag
    }
  }
  case class Fist(name1:String,value1:Int){
    var name:String = name1;
    var value = value1
   /* def this(name: String, value: Int) {
      //每个辅助构造器必须以主构造器或其他辅助构造器的调用作为第一句代码
      this(name, value)
    }*/
  }
}
